from crewai import Agent, Crew, Process, Task
from crewai.project import CrewBase, agent, crew, task

# Uncomment the following line to use an example of a custom tool
# from crewaiwithqwen.tools.custom_tool import MyCustomTool

# Check our tools documentations for more information on how to use them
# from crewai_tools import SerperDevTool

import os
os.environ["OPENAI_API_BASE"] = 'http://localhost:11434/v1' 
os.environ["OPENAI_API_KEY"] ='NA'

from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="ollama/qwen2.5:7b")

# 一个基础的Crew类
@CrewBase
class CrewaiwithqwenCrew():
	"""Crewaiwithqwen crew"""
	# 申明智能体
	# 教学大纲分析师
	@agent
	def syllabusReader(self) -> Agent:
		return Agent(
			config=self.agents_config['syllabusReader'],
			# tools=[MyCustomTool()], # Example of custom tool, loaded on the beginning of file
			verbose=True,
			llm=llm
		)

	# 选择题出题专家
	@agent
	def choiceQuestionMaker(self) -> Agent:
		return Agent(
			config=self.agents_config['choiceQuestionMaker'],
			verbose=True,
			llm=llm
		)

	# 编程题出题专家
	@agent
	def programmingQuestionMaker(self) -> Agent:
		return Agent(
			config=self.agents_config['programmingQuestionMaker'],
			verbose=True,
			llm=llm
		)

	# 报告批改专家
	@agent
	def reportCorrectionSpecialists(self) -> Agent:
		return Agent(
			config=self.agents_config['programmingQuestionMaker'],
			verbose=True,
			llm=llm
		)
	
	# 声明任务
	## 章节 -> 知识点
	@task
	def chapter_to_knowledge_points_task(self) -> Task:
		return Task(
			config=self.tasks_config['chapter_to_knowledge_points_task'],
			llm=llm
		)

	# 知识点 -> 选择题目
	@task
	def chapter_to_choice_questions_task(self) -> Task:
		return Task(
			config=self.tasks_config['chapter_to_choice_questions_task'],
			output_file='outputs/report_choiceQuestions.md',
			llm=llm
		)

	# 知识点 ->编程题目
	@task
	def chapter_to_programming_question_task(self) -> Task:
		return Task(
			config=self.tasks_config['chapter_to_programming_question_task'],
			output_file='outputs/report_programmingQuestions.md',
			llm=llm
		)

	# 生成报告
	@task
	def report_correction_task(self) -> Task:
		return Task(
			config=self.tasks_config['report_correction_task'],
			output_file='outputs/report_Docx批改.md',
			llm=llm
		)
	
	@crew
	def crew(self) -> Crew:
		"""Creates the Crewaiwithqwen crew"""
		return Crew(
			agents=self.agents, # Automatically created by the @agent decorator
			tasks=self.tasks, # Automatically created by the @task decorator
			process=Process.sequential,
			verbose=True,
			function_calling_llm=llm
			# process=Process.hierarchical, # In case you wanna use that instead https://docs.crewai.com/how-to/Hierarchical/
		)
	
# 根据需求出选择题的Crew类
@CrewBase
class CrewaiToCHOOSEQustions():
	"""Crewaiwithqwen crew"""
	# 申明智能体
	# 教学大纲分析师
	@agent
	def syllabusReader(self) -> Agent:
		return Agent(
			config=self.agents_config['syllabusReader'],
			# tools=[MyCustomTool()], # Example of custom tool, loaded on the beginning of file
			verbose=True,
			llm=llm
		)

	# 选择题出题专家
	@agent
	def choiceQuestionMaker(self) -> Agent:
		return Agent(
			config=self.agents_config['choiceQuestionMaker'],
			verbose=True,
			llm=llm
		)
	
	# 编程题出题专家
	@agent
	def programmingQuestionMaker(self) -> Agent:
		return Agent(
			config=self.agents_config['programmingQuestionMaker'],
			verbose=True,
			llm=llm
		)
	

	# 声明任务
	## 章节 -> 知识点
	@task
	def chapter_to_knowledge_points_task(self) -> Task:
		return Task(
			config=self.tasks_config['chapter_to_knowledge_points_task'],
			llm=llm
		)

	# 知识点 -> 选择题目
	@task
	def chapter_to_choice_questions_task(self) -> Task:
		return Task(
			config=self.tasks_config['chapter_to_choice_questions_task'],
			output_file='outputs/report_choiceQuestions.md',
			llm=llm
		)
	
	@crew
	def crew(self) -> Crew:
		"""Creates the Crewaiwithqwen crew"""
		return Crew(
			agents=self.agents, # Automatically created by the @agent decorator
			tasks=self.tasks, # Automatically created by the @task decorator
			process=Process.sequential,
			verbose=True,
			output_file='outputs/report_Docx批改.md',
			function_calling_llm=llm
			# process=Process.hierarchical, # In case you wanna use that instead https://docs.crewai.com/how-to/Hierarchical/
		)